package com.igor.bookStore.tapestry.pages;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.igor.bookStore.asobject.PurchaseItemBasketAS;
import com.igor.bookStore.beans.PurchaseItemBean;
import com.igor.bookStore.inface.Book;
import com.igor.bookStore.inface.PurchaseItem;
import com.igor.bookStore.service.Books;

public class BasketEntry {
	
	
	  @Property
      @Persist
      private int quantity;

      @Inject
      private Books books;

      // ASO 
      @ApplicationState
      private PurchaseItemBasketAS purchaseItemBasketAS;

      @Persist
      private Book chosenBook;

      public void setChosenBook(Book chosenBook) {
              this.chosenBook = chosenBook;
      }
      public Book getChosenBook() {
              return chosenBook;
      }

      Object onActivate(String bookName)
      {
              chosenBook = books.retrieveSingleBook(bookName);
              if (chosenBook == null) return EntryPurchase.class;
              return null;
      }
      
      @OnEvent (value="submit", component="basketEntryForm")
      Object onFromSubmit() {
    	  PurchaseItem book = new PurchaseItemBean();
              book.setId(chosenBook.getName());
              book.setQuantity(this.quantity);
              book.setTotalPrice(this.quantity * chosenBook.getPrice());
              book.setCanceled(false);
              book.setBook(chosenBook);

              purchaseItemBasketAS.getPurchaseItemBasket().put(book.getId(), book);
              return EntryPurchase.class;             
      }       

}
